import Vue from "vue";
import Vuex from "vuex";
import { DBUser, DBTodo } from "@/db";
Vue.use(Vuex);
const user = {
  namespaced: true,
  state: {
    user: DBUser.getCurrUser()
  },

  mutations: {
    SET_USER: (state, user) => {
      state.user = user;
      DBUser.login(user);
    },
    DEL_USER: state => {
      state.user = {};
      DBUser.loginOut();
    }
  },

  actions: {
    Login({ commit }, user) {
      commit("SET_USER", user);
    },
    LogOut({ commit, state }) {
      commit("DEL_USER");
    }
  }
};

const todo = {
  namespaced: true,
  state: {
    list: DBTodo.getList()
  },
  mutations: {
    ADD_TODO: (state, todo) => {
      DBTodo.addTodo(todo);
      state.list = DBTodo.getList();
    },
    TODO_SUCCESS(state, todoId) {
      DBTodo.todoSuccess(todoId);
      state.list = DBTodo.getList();
    },
    TODO_NOT(state, todoId) {
      DBTodo.todoNot(todoId);
      state.list = DBTodo.getList();
    },
    TODO_DEL(state, todoId) {
      DBTodo.delTodo(todoId);
      state.list = DBTodo.getList();
    },
    UPDATE_TODO(state, todo) {
      DBTodo.updateTodo(todo);
      state.list = DBTodo.getList();
    }
  }
};

export default new Vuex.Store({
  modules: {
    user,
    todo
  }
});
